function varargout = TLInportDlg(action,varargin)

% TL_INPORT_DLG manages the user interface for TargetLink Inport block
%
% SYNTAX dlgInfo = TLInportDlg('GetDlgInfo');
%   gets dialog info of TargetLink Inport block
%
%   INPUT ARGUMENTS
%   -/-      
%
%   OUTPUT ARGUMENTS
%   dlgInfo     dialog information contains pageNames etc.
%
% SYNTAX [sheetDims, ctrlPos] = TLInportDlg('CtrlPositions', pageName, commonGeom, dlgInfo, sheetDims);
%   calculates the control positions of all controls of dialog page: <pageName>
%
%   INPUT ARGUMENTS
%   pageName        name of current page
%   commonGeom      common geometry data
%   dlgInfo         dialog information contains pageNames etc.
%   sheetDims       dimension of sheet
%
%   OUTPUT ARGUMENTS
%   sheetDims       modified dimension of sheet
%   ctrlPos         positions of controls
%
% SYNTAX dlgdata = TLInportDlg('Create', pageName, dlgfig, dlgdata);
%   creates all controls of dialog page: <pageName>
%
%   INPUT ARGUMENTS
%   pageName        name of current page
%   dlgfig          figure handle
%   dlgdata         dialog data
%
%   OUTPUT ARGUMENTS
%   dlgdata         modified dialog data
%
% SYNTAX [dlgdata,bModified] = TLInportDlg('Manage', pageName, dlgfig, dlgdata, pageAction);
%   manages all actions of dialog page: <pageName>
%
%   INPUT ARGUMENTS
%   pageName        name of current page
%   dlgfig          figure handle
%   dlgdata         dialog data
%   pageAction      modified control of current page
%
%   OUTPUT ARGUMENTS
%   dlgdata         modified dialog data
%   bModified       flag = 1, if dialog data are modified, =0 otherwise
%
% SYNTAX dlgdata = TLInportDlg('Update', dlgfig, dlgdata, pageNum);
%   updates all controls of dialog page: <pageNum>
%
%   INPUT ARGUMENTS
%   dlgfig          figure handle
%   dlgdata         dialog data
%   pageNum         number of current page
%
%   OUTPUT ARGUMENTS
%   dlgdata         modified dialog data
%
% SYNTAX TLInportDlg('Apply', dlgfig, dlgdata);
%   writes current blockdata to block and DD
%
%   INPUT ARGUMENTS
%   dlgfig      handle for dialog figure
%   dlgdata     dialog data
%
%   OUTPUT ARGUMENTS
%   - / -


% Author(s): D. Andoleit, T. Pietzsch
% $RCSfile: TLInportDlg.m $
% $ProjectName: e:/ARC/Components/HostSW/SimulinkBlocksets/TLBlocksets/CommonBlockset/CommonBlockset_SimBlocksUI/Sources/MSrc/project.pj $
% $Revision: 1.27 $
% $Date: 2011/09/29 14:12:05MESZ $
%
% Copyright ?2011 dSPACE GmbH. All rights reserved.


switch action
    case 'GetDlgInfo'
        hBlock = varargin{1};
        dlgInfo = tl_get_dlginfodefaults('tlsim');
        titleName = 'Inport';
        pathinfo = tl_pathinfo(hBlock);
        dlgInfo.title = sprintf('TargetLink %s: %s', titleName, pathinfo.shortDisplayName);
        dlgInfo.pageNames = {
            'Output';
            'Logging & Autoscaling';
            'RTOS';
            'AUTOSAR';
            'Documentation'};
        % icon of switch for dual edit of output and AUTOSAR data
        dlgInfo.bDualEdit = 1;
        % get block data with codegen mode 'AUTOSAR' to have blockspecific AUTOSAR data in effecivescaling
        [tlData, extdata] = tl_get_effectivedata(hBlock);
        % set defaultPageNum to AUTOSAR page if AUTOSAR is enabled
        if extdata.bAutosarMode
            dlgInfo.defaultPageNum = 4;
        else
            dlgInfo.defaultPageNum = 1;
        end
        % initialize extended data
        dlgInfo.extdata.codegenmode = extdata.codegenmode;
        dlgInfo.extdata.bAutosarMode = extdata.bAutosarMode;
        dlgInfo.extdata.bIsEditMode = false;
        varargout{1} = dlgInfo;
        varargout{2} = tlData;

    case 'CtrlPositions',
        pageName    = varargin{1};
        commonGeom  = varargin{2};
        sheetDims   = varargin{4};
        % port dialog may be opened with bus port data and bCodeComment = 0
        switch pageName
            case 'Output'
                [sheetDims, ctrlPos] = FcnCalcOutputControlPositions(commonGeom);

            case 'Logging & Autoscaling'
                excludedCtrls = {'keepParameter'};
                [sheetDims, ctrlPos] = ...
                    TLManageLoggingCtrls('calcCtrls', commonGeom, sheetDims, excludedCtrls);

            case 'RTOS'
                excludedCtrls = {};
                [sheetDims, ctrlPos] = TLManageMessageCtrls('ctrlpos', commonGeom, sheetDims, excludedCtrls);

            case 'AUTOSAR'
                [sheetDims, ctrlPos] = TLManageAutosarCtrls('ctrlpos', commonGeom, sheetDims);

            case 'Documentation'
                bTblBlock = 0;
                bCodeComment = 1;
                [sheetDims, ctrlPos] = ...
                    TLManageDocCtrls('ctrlpos', commonGeom, ...
                    sheetDims, bCodeComment, bTblBlock);
        end
        varargout{1} = sheetDims;
        varargout{2} = ctrlPos;

    case 'Create'
        pageName = varargin{1};
        dlgfig = varargin{2};
        dlgdata = varargin{3};
        % create a hyperlink to transform the block to a normal port
        if ~isfield(dlgdata.SystemButtons, 'TransformLink') && ~dlgdata.bLocked 
            dlgdata = FcnCreateTransformLink(dlgfig, dlgdata);
        end
        pageNum  = strmatch(pageName, dlgdata.pageNames, 'exact');
        switch pageName
            case 'Output'
                dlgdata = FcnCreateOutputControls(pageNum, dlgfig, dlgdata);
                userData = struct('min', -1, 'max', 2^31-1);
                set(dlgdata.pageData{pageNum}.children.widthEdit, 'UserData', userData);
                userData = struct('min', 1, 'max', 2^31-1);
                set(dlgdata.pageData{pageNum}.children.elementEdit , 'UserData', userData);

            case 'Logging & Autoscaling'
                dlgdata = TLManageLoggingCtrls('create', pageNum, dlgfig, dlgdata);

            case 'RTOS'
                dlgdata = TLManageMessageCtrls('create', pageNum, dlgfig, dlgdata);

            case 'AUTOSAR'
                dlgdata = TLManageAutosarCtrls('create', pageNum, dlgfig, dlgdata);

            case 'Documentation'
                dlgdata = ...
                    TLManageDocCtrls('create', pageNum, dlgfig, dlgdata);
        end
        dlgdata.pageData{pageNum}.ctrlPos = [];
        varargout{1} = dlgdata;

    case 'Manage'
        pageName   = varargin{1};
        dlgfig     = varargin{2};
        dlgdata    = varargin{3};
        pageAction = varargin{4};
        pageNum    = strmatch(pageName,dlgdata.pageNames,'exact');
        switch pageName
            case 'Output'
                [dlgdata, bModified] = ...
                    FcnManageOutputPage(dlgfig, dlgdata, pageAction);

            case 'Logging & Autoscaling'
                [dlgdata, bModified] = ...
                    TLManageLoggingCtrls('manage', pageNum, dlgdata, pageAction, 'logdata');

            case 'RTOS'
                [dlgdata, bModified] = TLManageMessageCtrls( ...
                    'manage', pageNum, dlgdata, pageAction);

            case 'AUTOSAR'
                [dlgdata, bModified] = ...
                    TLManageAutosarCtrls( 'manage', pageNum, dlgdata, pageAction, 0);

            case 'Documentation'
                [dlgdata, bModified] = ...
                    TLManageDocCtrls('callback', pageNum, dlgfig, ...
                    dlgdata, pageAction);
        end
        varargout{1} = dlgdata;
        varargout{2} = bModified;

    case 'ManageSystemIcons',
        dlgfig     = varargin{1};
        dlgdata    = varargin{2};
        pageAction = varargin{3};
        [dlgdata, bModified] = FcnManageSystemIcons(dlgfig,dlgdata,pageAction);
        varargout{1} = dlgdata;
        varargout{2} = bModified;

    case 'Update'
        dlgfig  = varargin{1};
        dlgdata = varargin{2};
        pageNum = varargin{3};

        switch pageNum
            case 1
                % Output page
                dlgdata = FcnUpdateOutputPage(dlgfig, dlgdata, pageNum);

            case 2
                % Logging & Autoscaling
                dlgdata =  TLManageLoggingCtrls('update',pageNum,dlgdata,'logdata','output');
                TLManageLoggingCtrls('setCtrls',pageNum, dlgfig, dlgdata);

            case 3
                % RTOS
                dlgdata =  TLManageMessageCtrls('update',pageNum, dlgdata);
                TLManageMessageCtrls('setCtrls', pageNum, dlgfig, dlgdata);

            case 4
                % AUTOSAR
                excludedKinds = {'OperationReturnValue'};
                dlgdata =  TLManageAutosarCtrls('update',pageNum, dlgfig, dlgdata, 0, excludedKinds);
                TLManageAutosarCtrls('setCtrls', pageNum, dlgfig, dlgdata);

            case 5
                % Documentation page
                TLManageDocCtrls('update', pageNum, dlgfig, dlgdata);
        end
        if ~dlgdata.bLocked
            % set windowButtonMotionFcn if hyperlink exists
            set(dlgfig, 'WindowButtonMotionFcn', {@FcnWindowButtonMotion, dlgdata.SystemButtons.TransformLink});
        end
        varargout = {dlgdata};

    case 'Apply'
        dlgdata = varargin{2};
        if strcmp(get_param(dlgdata.model,'MaskType'), 'TL_BusInport')
            % set correct maskType
            set_param(dlgdata.model,'MaskType', 'TL_Inport')
        end

        bClose   = varargin{3};
        [dlgdata, bClose] = tl_apply_blockdata(dlgdata, bClose);
        varargout{1} = dlgdata;
        varargout{2} = bClose;

    otherwise
        error(['Invalid action "' action '"']);
end


%-----------------------------------------------------------------------------------------
% FcnCreateTransformLink creates a hyperlink that transforms the block into a busport
%
% INPUT ARGUMENTS
%   dlgfig      handle for dialog figure
%   dlgdata     dialog data
%
% OUTPUT ARGUMENTS
%   dlgdata     modified dialog data
%-----------------------------------------------------------------------------------------
function dlgdata = FcnCreateTransformLink(dlgfig, dlgdata)

str = 'Click here to transform the port block into a busport block';
pos = get(dlgdata.SystemButtons.DateLabel, 'Position');
strings_and_styles = {['   ', str, '   '],   'text'};
transformLinkSize = ds_get_uicontrol_sizes(dlgdata.commonGeom, strings_and_styles);
transformLinkPos = [dlgdata.commonGeom.frameSideEdgeBuffer, pos(2), transformLinkSize];
dlgdata.SystemButtons.TransformLink = uicontrol( ...
    'Parent',             dlgfig, ...
    'Style',              'text', ...
    'ForegroundColor',    'blue', ...
    'FontAngle',          'italic', ...
    'Enable',             'inactive', ...
    'String',             str, ...
    'Position',           transformLinkPos, ...
    'ButtonDownFcn',      @FcnTransformPort);


%-----------------------------------------------------------------------------------------
% FcnWindowButtonMotion manages the variable control callbacks
%
% INPUT ARGUMENTS
%   hObject         handle to figure1 (see GCBO)
%   eventdata       reserved - to be defined in a future version of MATLAB
%   hTransformLink  handle of link control
%
% OUTPUT ARGUMENTS
%   - / -
%-----------------------------------------------------------------------------------------
function FcnWindowButtonMotion(hObject, eventdata, hTransformLink)      %#ok<INUSL>

if ~isempty(hTransformLink)
    cp = get(hObject, 'currentpoint');
    pos = get(hTransformLink, 'position');
    bIsOverPosition = inpolygon(cp(1), cp(2), [pos(1) pos(1) pos(1)+pos(3) pos(1)+pos(3)], ...
        [pos(2) pos(2)+pos(4) pos(2)+pos(4) pos(2)]);
    if bIsOverPosition
        % set pointer to 'hand'
        set(hObject, 'Pointer', 'hand')
    else
        % reset pointer
        set(hObject, 'Pointer', 'arrow')
    end
end


%-----------------------------------------------------------------------------------------
% FcnTransformPort creates a hyperlink that trnasforms the block into normal port
%
% INPUT ARGUMENTS
%   src         source of button down callback
%   event       event which triggered the callback
%
% OUTPUT ARGUMENTS
%   -/-
%-----------------------------------------------------------------------------------------
function FcnTransformPort(src, event) %#ok<INUSD>

% let user confirm data loss
msg = 'Note that all TargetLink settings will be lost by transforming the port block into a busport block. Continue anyway?';
% make sure, that question is invoked
savedBatchMode = ds_error_get('batchmode');
ds_error_set('batchmode', 'off');
[btn, h] = ds_msgbox( ...
    'Message',      msg, ...
    'Style',        'question', ...
    'Button',       {'OK','Cancel'}, ...
    'Default',      'OK', ...
    'Close',        'manual');
ds_error_set('batchmode', savedBatchMode);
while true
    if ~ishandle(h)
        return
    elseif strcmp(btn, 'Cancel')
        delete(h);
        return
    elseif strcmp(btn, 'OK')
        delete(h);
        break
    end
end

% get dialog data struct
dlgfig = get(src, 'Parent');
dlgdata = get(dlgfig, 'UserData');

% substitute masktype and reset blockdata
set_param(dlgdata.model, 'MaskType', 'TL_BusInport');
set_param(dlgdata.model, 'data', '{''date'', 730486}');

% close old dialog window and open block dialog for new blocktype
delete(dlgfig);
open_system(dlgdata.model);


%-----------------------------------------------------------------------------------------
% FcnManageSystemIcons manage actions of toolbar icons
%
% INPUT ARGUMENTS
%   dlgfig      handle for dialog figure
%   dlgdata     dialog data
%   action      action at one control of current page
%
% OUTPUT ARGUMENTS
%   dlgdata     modified dialog data
%   bModified   flag = 1, if dialog data are modified, =0 otherwise
%-----------------------------------------------------------------------------------------
function [dlgdata, bModified] = FcnManageSystemIcons(dlgfig, dlgdata, action) %#ok

bModified = false;
switch action
    case 'EditCode'
        % look for associated production code file
        [codefile,blockstr] = get_codefile(dlgdata.model);
        if ~isempty(codefile)
            codeedit(codefile,blockstr);
        end

    case 'ViewCode'
        tl_create_links_to_model('ShowComputation',dlgdata.model, 'output');

    case 'DualEdit'
        dlgdata.extdata.bIsEditMode = strcmp(get(dlgdata.SystemButtons.DualEdit, 'State'), 'on');
        bModified = true;

    otherwise
        error('Invalid callback');

end % switch(action)


%-----------------------------------------------------------------------------------------
% FcnCalcOutputControlPositions calculates positions for all controls on output page
%
% INPUT ARGUMENTS
%   commonGeom      common geometry information
%
% OUTPUT ARGUMENTS
%   sheetDims       modified dimension of sheet
%   ctrlPos         positions of controls
%
% HINT:
% Positions are calculated from the bottom left of the figure to avoid
% dependencies on figure size
%-----------------------------------------------------------------------------------------
function [sheetDims, ctrlPos] = FcnCalcOutputControlPositions(commonGeom)

excludedCtrls = {'saturate'};
[sheetDims, ctrlPos, col] = ...
    tl_calc_ctrls_positions(commonGeom, excludedCtrls);

% message of dual edit mode
textWidth = ctrlPos.tlcgoptGroupbox(3);
textHeight = commonGeom.textHeight;

cx = col(1) - commonGeom.frameSideEdgeBuffer;
cy = ctrlPos.tlcgoptGroupbox(2) + ctrlPos.tlcgoptGroupbox(4) ...
    + commonGeom.textHeight;

% text frame
ctrlPos.dualEditText = [cx, cy, textWidth, textHeight];
sheetDims(2) = sheetDims(2) ...
    + textHeight;


%-----------------------------------------------------------------------------------------
% FcnCreateOutputControls creates the figure window
%
% INPUT ARGUMENTS
%   pageNum     number of current page
%   dlgfig      handle for dialog figure
%   dlgdata     dialog data
%
% OUTPUT ARGUMENTS
%   dlgdata     modified dialog data
%-----------------------------------------------------------------------------------------
function dlgdata = FcnCreateOutputControls(pageNum, dlgfig, dlgdata)

dlgdata = TLManageVariableCtrls('create', pageNum, dlgfig, dlgdata);
dlgdata = TLManageScalingCtrls('create', pageNum, dlgfig, dlgdata, 'output', 1);

% prepare some data
ctrlPos  = dlgdata.pageData{pageNum}.ctrlPos;
Children = dlgdata.pageData{pageNum}.children;

% correct position of dual edit message; must be the same on both tabs
if isempty(dlgdata.pageData{4}.children)
    % AUTOSAR page not yet created
    ctrlPos.dualEditText = dlgdata.pageData{4}.ctrlPos.dualEditText;
else
    ctrlPos.dualEditText = get(dlgdata.pageData{4}.children.dualEditText, 'Position');
end

% create dualEditText
Children.dualEditText = uicontrol( ...
    'Parent',             dlgfig, ...
    'Style',              'text', ...
    'String',             ' ', ...
    'Position',           ctrlPos.dualEditText);

% rewrite children data
dlgdata.pageData{pageNum}.children = Children;


%-----------------------------------------------------------------------------------------
% FcnManageOutputPage manage control actions of output page
%
% INPUT ARGUMENTS
%   pageNum     number of current page
%   dlgdata     dialog data
%   pageAction  action at one control of current page
%
% OUTPUT ARGUMENTS
%   dlgdata     modified dialog data
%   bModified   flag = 1, if dialog data are modified, =0 otherwise
%-----------------------------------------------------------------------------------------
function [dlgdata, bModified] = FcnManageOutputPage(dlgfig, dlgdata, pageAction)

pageNum = 1;
Children = dlgdata.pageData{pageNum}.children;

switch pageAction
    case 'widthEdit'
        [displayedWidthChar, bModified] = tl_handle_editupdown( 'Manage', ...
            num2str(dlgdata.data.output.width), ...
            [Children.widthEdit Children.widthUpButton Children.widthDownButton], ...
            Children.(pageAction));
        displayedWidth = str2double(displayedWidthChar);
        if displayedWidth ~= 0 && ~isempty(displayedWidth) ...
                && ~isequalwithequalnans(displayedWidth, NaN)
            dlgdata.data.output.width = displayedWidth;
        end
        if bModified && ~isempty(displayedWidth) ...
                && ~isequalwithequalnans(displayedWidth, NaN)
            % ensure that the element-value is <= the width (excepted width=-1)
            currentElement = min(dlgdata.extdata.output.currentElement,displayedWidth);
            if currentElement >= 1
                dlgdata.extdata.output.currentElement = currentElement;
            else
                dlgdata.extdata.output.currentElement = 1;
            end
        end

    case {'widthUpButton', 'widthDownButton'}
        [displayedWidthChar, bModified] = tl_handle_editupdown( 'Manage', ...
            num2str(dlgdata.data.output.width), ...
            [Children.widthEdit Children.widthUpButton Children.widthDownButton], ...
            Children.(pageAction));
        displayedWidth = str2double(displayedWidthChar);
        if displayedWidth == 0
            [displayedWidthChar, bModified] = tl_handle_editupdown( 'Manage', ...
                displayedWidthChar, ...
                [Children.widthEdit Children.widthUpButton Children.widthDownButton], ...
                Children.(pageAction));
            displayedWidth = str2double(displayedWidthChar);
        end
        if bModified
            % ensure that the element-value is <= the width (excepted width=-1)
            currentElement = min(dlgdata.extdata.output.currentElement,displayedWidth);
            if currentElement >= 1
                dlgdata.extdata.output.currentElement = currentElement;
            else
                dlgdata.extdata.output.currentElement = 1;
            end
        end
        dlgdata.data.output.width = displayedWidth;

    otherwise
        [dlgdata, bModified2] = TLManageVariableCtrls( ...
            'manage', pageNum, dlgfig, dlgdata, pageAction, 'output');
        [dlgdata, bModified3] = TLManageScalingCtrls( ...
            'manage', pageNum, dlgfig, dlgdata, pageAction, 'output');
        bModified = bModified2 || bModified3;
end


%-----------------------------------------------------------------------------------------
% FcnUpdateOutputPage updates output page
%
% INPUT ARGUMENTS
%   dlgdata     dialog data
%   dlgfig      figure handle of dialog
%   pageNum     page Number
%
% OUTPUT ARGUMENTS
%   dlgdata     modified dialog data
%-----------------------------------------------------------------------------------------
function dlgdata = FcnUpdateOutputPage(dlgfig, dlgdata, pageNum)

if dlgdata.extdata.bIsEditMode
    msg = '   The dual edit mode is active to visualize both standard as well as AUTOSAR code generation settings. ';
    set(dlgdata.pageData{pageNum}.children.dualEditText, ...
        'String', msg, ...
        'Enable', 'on', ...
        'ForegroundColor',    'w', ...
        'BackgroundColor',    [.9 .5 0] ...    % orange
        );
else
    set(dlgdata.pageData{pageNum}.children.dualEditText, ...
        'String', ' ', ...
        'ForegroundColor',    'b', ...
        'BackgroundColor',    get(0,'DefaultUicontrolBackgroundColor') ...
        );
end

Children = dlgdata.pageData{pageNum}.children;

if dlgdata.data.autosar.useautosarcommunication && any(dlgdata.extdata.codegenmode == [0 2]) && ~dlgdata.extdata.bIsEditMode
    % AUTOSAR mode is activated, disable all output controls
    ctrlNames = fieldnames(Children);
    ctrlNames = ctrlNames(cellfun('isempty', regexp(ctrlNames, 'Autosar', 'match')));
    for k = 1:numel(ctrlNames)
        eval(['hChild = Children.', ctrlNames{k}, ';']);
        % There are some control names which are associated to more than
        % one handle. Thus use following loop.
        for m = 1:numel(hChild)
            tmpUserData = get(hChild(m), 'UserData');
            tmpUserData.ctrlStatus.enable = 'off';
            set(hChild(m), 'UserData', tmpUserData);
        end            
    end
    TLSetCtrlsStatus(dlgfig, Children, ctrlNames);
else
    % invoke standard update function
    dlgdata = TLManageVariableCtrls('update', pageNum, dlgdata, 'output');
    dlgdata = TLManageScalingCtrls('update', pageNum, dlgdata, 'output', []);

    if ~dlgdata.data.output.inheritscaling
        % handle width controls
        widthEditCtrlStatus = tl_get_control_userdata(Children.widthEdit);
        widthUpButtonCtrlStatus = tl_get_control_userdata(Children.widthUpButton);
        widthDownButtonCtrlStatus = tl_get_control_userdata(Children.widthDownButton);
        scalarExpCheckboxCtrlStatus = tl_get_control_userdata(Children.scalarExpCheckbox);
        if dlgdata.pageData{pageNum}.bWritable.width
            % width is not locked by a DD variable, so it should be modifiable
            widthEditCtrlStatus.enable = 'on';

            % can be -1, meaning 'inherit width' just like in SL
            widthEditCtrlStatus.string = num2str(dlgdata.data.output.width);
            widthUpButtonCtrlStatus.enable = 'on';
            if dlgdata.data.output.width > -1
                widthDownButtonCtrlStatus.enable = 'on';
            end
            if abs(dlgdata.data.output.width) == 1
                % deactivate and disable same scaling
                scalarExpCheckboxCtrlStatus.value = 0;
                scalarExpCheckboxCtrlStatus.enable = 'inactive';
            else
                % width > 1
                if dlgdata.extdata.output.scalarExp
                    scalarExpCheckboxCtrlStatus.value = 1;
                else
                    scalarExpCheckboxCtrlStatus.value =  ...
                        all((dlgdata.data.output.lsb(2:end)-dlgdata.data.output.lsb(1:end-1)) == 0) && ...
                        all((dlgdata.data.output.offset(2:end)-dlgdata.data.output.offset(1:end-1)) == 0) && ...
                        all((dlgdata.data.output.min(2:end)-dlgdata.data.output.min(1:end-1)) == 0) && ...
                        all((dlgdata.data.output.max(2:end)-dlgdata.data.output.max(1:end-1)) == 0) && ...
                        all((dlgdata.data.output.arb(2:end)-dlgdata.data.output.arb(1:end-1)) == 0);
                end
            end
        end

        % overwrite controls user data
        ctrlsList = {'widthEdit', 'widthDownButton', 'widthUpButton', 'scalarExpCheckbox'};
        % write back control status of controls to the controls' UserData
        for ctrlsNo = 1:length(ctrlsList)
            ctrl = Children.(ctrlsList{ctrlsNo});
            UserData = get(ctrl, 'UserData');
            UserData.ctrlStatus = eval([ctrlsList{ctrlsNo} 'CtrlStatus']);
            set(ctrl, 'UserData', UserData);
        end
    end

    TLManageVariableCtrls('setCtrls', pageNum, dlgfig, dlgdata);
    TLManageScalingCtrls('setCtrls', pageNum, dlgfig, dlgdata);
end
